package sfin.service;


import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.ResourceBundle;

import sfin.swt.util.FormatterUtil;

public class PeriodoService extends AbstractService{

	List<Integer> years = new ArrayList<Integer>();
	String[] months = new String[12];
	
	private Calendar calendar;
	
	public PeriodoService(ResourceBundle bundle, FormatterUtil formatterUtil) {
		super(bundle,formatterUtil);
		this.calendar = formatterUtil.getCalendarInstance();
		initializeYearList();
		initializeMonthList();
	}

	private void initializeMonthList() {
		this.months = formatterUtil.getMonthNames();
	}

	public Calendar getCalendar() {
		return calendar;
	}
	
	public int getLastDayOfMonth(){
		return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
	}
	
	private void initializeYearList() {
		years.add(2011);
		years.add(2012);
		years.add(2013);
		years.add(2014);
		years.add(2015);
		years.add(2016);
	}

	public int getAt(int index){
		return years.get(index);
	}
	
	public int indexOf(int year){
		return years.indexOf(year);
	}
	
	public String[] getMonths(){
		return months;
	}
	
	public Integer[] getYears(){
		return years.toArray(new Integer[years.size()]);
	}
	
	public String[] getYearNames(){
		Integer[] yearList = getYears();
		String[] years = new String[yearList.length];
		for(int i = 0; i<yearList.length; i++){
			years[i] = String.valueOf(yearList[i]);
		}
		return years;
	}
	
	
	public String getCurrentMonthName(){
		int actualMonth = getApplicationMonth();
		return months[ actualMonth ];
	}
	
	public void changeCurrentApplicationYearIndex(int index){
		this.calendar.set(Calendar.YEAR, years.get(index));
		notifyListeners();
	}
	
	public void changeCurrentApplicatioMonth(int month){
		this.calendar.set(Calendar.MONTH, month);
		notifyListeners();
	}
	
	public int getCurrentApplicationYearIndex() {
		return years.indexOf(calendar.get(Calendar.YEAR));
	}
	
	public int getApplicationYear(){
		return calendar.get(Calendar.YEAR);
	}
	
	public int getApplicationMonth(){
		return calendar.get(Calendar.MONTH);
	}
	
	public void next(){
		calendar.add(Calendar.MONTH, 1);
		notifyListeners();
	}
	
	public void back(){
		calendar.add(Calendar.MONTH, -1);
		notifyListeners();
	}
}